#include <QMouseEvent>
#include <QPainter>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "skin/skinwindow.h"
#include "common.h"
#include <QGraphicsDropShadowEffect>

MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 去掉边框
    setWindowFlags(Qt::FramelessWindowHint | windowFlags());
    setAttribute(Qt::WA_TranslucentBackground);
    // 设置窗口固定大小
    setFixedSize(940, 600);
    // 窗口阴影
//    QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect(this);
//    effect->setBlurRadius(10);
//    effect->setColor(Qt::black);
//    effect->setOffset(0, 0);
//    setGraphicsEffect(effect);

    // 标题栏初始化
    initTitleBar();

    setStyleSheet("color:white");
    setBkOpacity(100);
    m_bkImage = ":/images/湖水.jpg";
    Common::getInstance()->setMainWindow(this);
    Common::getInstance()->setBackgroundImage(m_bkImage);
    Common::getInstance()->setMainWinOpacity(100);
}

MainWindow::~MainWindow()
{
    delete ui;
}

// value == 0 -> 完全透明
// value == 255 -> 完全不透明
void MainWindow::setBkOpacity(int value)
{
    QString style  = QString("background-color: rgb(255, 255, 255, %1)").arg(value);
    ui->containerWg->setStyleSheet(style);
}

void MainWindow::setBkImage(QString name)
{
    m_bkImage = name;
    update();
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    // 如果鼠标左键按下
    if(event->button() == Qt::LeftButton)
    {
        // 鼠标按下位置 - 窗口左上角坐标的差值
        m_pt = event->globalPos() - this->geometry().topLeft();
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    // 如果是鼠标左键拖动窗口
    if(event->buttons() & Qt::LeftButton)
    {
        // 移动当前窗口
        move(event->globalPos() - m_pt);
    }
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);

    painter.drawPixmap(10, 10, this->width()-20, this->height()-20, QPixmap(m_bkImage));
}

// 初始化标题j栏
void MainWindow::initTitleBar()
{
    // 处理按钮信号
    connect(ui->titleBar, &TitleBar::closeWin, this, &MainWindow::close);
    connect(ui->titleBar, &TitleBar::minimumWin, this, &MainWindow::showMinimized);
    connect(ui->titleBar, &TitleBar::showMenu, [=]()
    {

    });
    connect(ui->titleBar, &TitleBar::skinChange, [=]()
    {
        SkinWindow *skinWin = new SkinWindow;
        skinWin->showWgAnimation();
    });
}
